/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.UI.WebControls;
using DotNetNuke.UI.Utilities;

using ArticleConfig = DotNetNuke.Modules.Articles.Common.ArticleSettings;
using Constants = DotNetNuke.Modules.Articles.Common.Constants;
using DotNetNuke.Modules.Articles.Framework;
using DotNetNuke.ModuleToolkit.Modules.Skinning.SkinObjects;
using DotNetNuke.ModuleToolkit.Controls;

#endregion

namespace DotNetNuke.Modules.Articles.UI.Editors
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ArticleEditor skin object is used to edit an article
    /// </summary>
    /// <history>
    /// 	[cnurse]	04/14/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:ArticleEditor runat=\"server\"> </{0}:ArticleEditor>")]
    public class ArticleEditor :
        EditorBase
    {

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Default Image Directory
        /// </summary>
        /// <history>
        ///     [cnurse]  07/02/2007  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected string DefaultImageDirectory
        {
            get
            {
                string imageDirectory = Null.NullString;
                if (Section != null)
                {
                    FolderController folderController = new FolderController();
                    FolderInfo defaultFolder = folderController.GetFolderInfo(ParentArticlesModule.PortalId, Section.DefaultImageDirectory);
                    if (defaultFolder != null)
                        imageDirectory = defaultFolder.FolderPath;
                }
                return imageDirectory;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the editor type
        /// </summary>
        /// <history>
        /// 	[cnurse]	10/26/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override string EditorType
        {
            get { return Constants.TYPE_Article; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Articles Module Base for this control
        /// </summary>
        /// <history>
        ///     [cnurse]  05/20/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected ArticlesModuleBase ParentArticlesModule
        {
            get { return (ArticlesModuleBase)ParentModule; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Section for the Article
        /// </summary>
        /// <history>
        ///     [cnurse]  07/02/2007  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected SectionInfo Section
        {
            get
            {
                SectionInfo section = null;
                if (Article != null)
                {
                    section = SectionController.GetSection(Article.SectionId);
                }
                return section;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets whether the Delete button is displayed
        /// </summary>
        /// <history>
        ///     [cnurse]  09/09/2007  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override bool ShowDelete
        {
            get
            {
                bool showDelete;
                //Users with Article Delete permission can Delete articles (if enabled)
                if (ParentArticlesModule.CanDeleteArticle)
                    showDelete = base.ShowDelete;
                else
                    showDelete = false;
                return showDelete;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets whether the Moderation is enabled
        /// </summary>
        /// <history>
        ///     [cnurse]  10/10/2007  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override bool UseModeration
        {
            get
            {
                bool useModeration = Null.NullBoolean;
                if (ParentArticlesModule != null)
                {
                    ArticleConfig config = ArticleConfig.GetSettings(ParentArticlesModule.ModuleId, ParentArticlesModule.TabModuleId);
                    useModeration = config.UseModeration;
                }
                return useModeration;
            }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Article for this Editor
        /// </summary>
        /// <history>
        ///     [cnurse]  07/28/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        public ArticleInfo Article
        {
            get { return (ArticleInfo)DataSource; }
            set { DataSource = value; }
        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CreateEditor creates the PropertyEditor
        /// </summary>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	09/09/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void CreateEditor()
        {
            base.CreateEditor();

            foreach (FieldEditorControl editor in Fields)
            {
                FileEditControl fileEditor = editor.Editor as FileEditControl;
                if (fileEditor != null && fileEditor.Name == "AbstractImage")
                {
                    fileEditor.FilePath = DefaultImageDirectory;
                }
            }
        }

        #endregion

    }
}
